/* eslint-disable keyword-spacing,indent */
// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.home {
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
    this.tactive = 'gongxufabu';
  }
  // 订单列表
  async listAction () {
    const shangpingleibie = this.get('shangpingleibie');
    const mokuaibiaoshi = this.get('mokuaibiaoshi');
    console.log('shangpingleibie=' + shangpingleibie);
    const map = {};
    if (!think.isEmpty(shangpingleibie)) {
      map.shangpingleibie = ['like', '%' + shangpingleibie + '%'];
    }
    if (!think.isEmpty(mokuaibiaoshi)) {
      map.mokuaibiaoshi = mokuaibiaoshi;
    }
    const q = this.get('q');
    if (!think.isEmpty(q)) {
      map['order_no|user_id|accept_name|mobile'] = ['like', q];
    }
    // this.config("db.nums_per_page",20)
    const data = await this.model('gongxufabu').where(map).page(this.get('page') || 1, 10).order('id desc').countSelect();
    const html = this.pagination(data);
    this.assign('pagerData', html); // 分页展示使用
    // console.log(data.data);
    this.active = 'home/gongxufabu/list';
    this.assign('list', data.data);
    console.log('list=' + data.data);
    // this.meta_title = '发布管理';
    return this.display();
  }

  /**
   * 审核并分配业务内勤
   */
  async auditAction () {
    if (this.isPost) {
      console.log('+++++++++');
      const id = this.post('id');
      const phone = this.post('phone');
      const resfor = await this.model('api/member').where({
        mobile: phone
      }).count();
      if (resfor == 0) {
        return this.fail('此号码无对应业务内勤');
      }
      const shenheshijian = new Date().toLocaleString();
      const res = await this.model('gongxufabu').where({
        id: id
      }).update({
        neiqinmobile: phone,
        status: 1,
        shenheshijian: shenheshijian
      });
      return this.success({
        name: '指定业务内勤成功！'
      });
    } else {
      const id = this.get('id');
      this.assign('id', id);
      this.meta_title = '内勤分配';
      return this.display();
    }
  }

  /**
   * 作废订单
   */
  async voidAction () {
    if (this.isPost) {
      const id = this.post('id');
      const zuofei_remark = this.post('zuofei_remark');
      const zuofeishijian = new Date().toLocaleString();
      const voids = await this.model('gongxufabu').where({
        id: id
      }).update({
        status: 6,
        zuofei_remark: zuofei_remark,
        zuofeishijian: zuofeishijian
      });
      if (voids) {
        return this.success({
          name: '操作成功！',
          url: this.referer()
        });
      } else {
        return this.fail('操作失败！');
      }
    } else {
      const id = this.get('id');
      this.assign('id', id);
      this.meta_title = '审核订单';
      return this.display();
    }
  }

  /**
   * 直接释放发布信息
   */
  async shifangAction () {
    if (this.isPost) {
      const id = this.post('id');
      const shifang_remark = this.post('shifang_remark');
      const shifangshijian = new Date().toLocaleString();
      const shifang = await this.model('gongxufabu').where({
        id: id
      }).update({
        status: 5,
        shifang_remark: shifang_remark,
        shifangshijian: shifangshijian
      });
      if (shifang) {
        return this.success({
          name: '信息释放成功！'
        });
      } else {
        return this.fail('操作失败！');
      }
    } else {
      const id = this.get('id');
      this.assign('id', id);
      this.meta_title = '释放订单';
      return this.display();
    }
  }

  /**
   * 查看发布详情
   * @returns {*}
   */
  async detailAction () {
    const id = this.get('id');
    this.meta_title = '查看发布详情';

    // if (this.is_login > 0) {
    // 获取发布信息

    const gongxufabu = await this.model('gongxufabu').find({
      where: {
        id: id,
        _complex: {
          faburen: this.is_login,
          status: 5,
          _logic: 'or'
        }
      }
    });
    if (think.isEmpty(gongxufabu)) return this.redirect('/shangping');
    this.assign('gongxufabu', gongxufabu);
    this.active = ['/shangping?mokuaibiaoshi=' + gongxufabu.mokuaibiaoshi];
    const gongxuinfo = await this.model('gongxufabu').where({
      id: ['!=', id],
      status: 5
    }).order('id desc').limit(3).select();
    this.assign('gongxuinfo', gongxuinfo);
    const url = gongxufabu.tupian ? JSON.parse(gongxufabu.tupian) : [];
    this.assign('url', url);
    // 登录人
    let loginUser = null;
    if (this.is_login) {
      loginUser = await this.model('member').where({
        id: this.is_login
      }).find();
      this.user = think.extend(this.user, loginUser);
      await this.session('webuser', this.user);
    }
    // 发布人信息
    const user = await this.model('member').where({
      id: gongxufabu.faburen
    }).find();
    if (loginUser) {
      if (!(loginUser.groupid == 11 && user.groupid == 11) || loginUser.id == user.id) {
        this.assign('user', user);
      }
      if (loginUser.groupid == 11 && user.groupid == 11 && loginUser.id != user.id) {
        this.assign('showLiaoJieBotton', true);
      }
      if (loginUser.groupid != user.groupid && loginUser.id != user.id && gongxufabu.mokuaibiaoshi != 0) {
        this.assign('showXiaDanBotton', true);
      }
      if (loginUser.id == gongxufabu.faburen && gongxufabu.status == 0) {
        this.assign('showCancelBotton', true);
      }
    } else {
      this.assign('showLiaoJieBotton', true);
    }

    // } else {
    //   return this.redirect('/');
    // }

    return this.display();
  }

  /**
   * 下订单
   */
  async xiadanAction () {
    if (this.is_login > 0) {
      const id = this.post('id');
      const userid = this.is_login;
      const gongxuInfo = await this.model('gongxufabu').where({
        id: id,
        status: 5
      }).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('门户下单信息:供需信息为空：供需信息id:', id);
        return this.fail('下单失败');
      }
      if (gongxuInfo.mokuaibiaoshi == 0) return this.fail('下单失败-社区回收');
      const res = await this.model('gongxufabu').where({
        id: id
      }).update({
        status: 6
      });
      if (!res) {
        return this.fail('确认失败');
      }
      const orderId = await this.model('cmswing/order').gongtoorder(id, userid);
      if (!orderId) {
        return this.fail('订单生成失败');
      }
      return this.success({
        name: '下单成功'
      });
    } else {
      return this.fail('下单失败-未登录');
    }
  }
  /**
   * 撤销订单
   */
  async cancelAction () {
    if (this.is_login > 0) {
      const id = this.post('id');
      const gongxuInfo = await this.model('gongxufabu').where({ id: id, status: 0 }).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('cancelAction:供需信息为空:id', id);
        return this.fail('撤销失败');
      }
      const remarkData = this.post();
      remarkData.status = 6;
      remarkData.operation = '撤销';
      remarkData.remark_contents = '用户web端撤销挂单';
      remarkData.remark_type = 'center/fabu';
      const {
        remarksId,
        updateData
      } = await this.addremark();
      updateData.faburen_remarks_id = remarksId;
      const res = await this.model('gongxufabu').where({ id: id }).update(updateData);
      if (!res) { return this.fail('撤销失败') }
      return this.success({ name: '撤销成功', url: '/center/fabu' }, '撤销成功');
    } else {
      return this.fail('撤销失败');
    }
  }

  /**
   * 下架订单
   */
  async xiajiaAction () {
    if (this.is_login > 0) {
      const id = this.post('id');
      const gongxuInfo = await this.model('gongxufabu').where({ id: id, status: 5 }).find();
      if (think.isEmpty(gongxuInfo)) {
        think.logger.info('cancelAction:供需信息为空:id', id);
        return this.fail('下架失败');
      }
      const remarkData = this.post();
      remarkData.status = 6;
      remarkData.operation = '下架';
      remarkData.remark_contents = '用户web端下架挂单';
      remarkData.remark_type = 'center/fabu';
      const {
        remarksId,
        updateData
      } = await this.addremark();
      updateData.offsale_remarks_id = remarksId;
      updateData.offsale_time = remarkData.remark_time;
      const res = await this.model('gongxufabu').where({ id: id }).update(updateData);
      if (!res) { return this.fail('下架失败') }
      return this.success({ name: '下架成功', url: '/center/fabu' }, '下架成功');
    } else {
      return this.fail('下架失败');
    }
  }
};
